package com.medical.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.system.common.Result;
import com.medical.system.model.Department;
import com.medical.system.service.DepartmentService;
import com.medical.system.vo.DepartmentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "科室控制器")
@RequestMapping("/admin/system/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation("分页和条件查询")
    @GetMapping("/{page}/{limit}")
    public Result selectClientPageByVo(@PathVariable Long page,
                                       @PathVariable Long limit,
                                       DepartmentVo departmentVo){
        IPage<Department> iPage = new Page<>(page,limit);

        iPage =   this.departmentService.selectPage(iPage,departmentVo);
        return Result.ok(iPage);
    }

    @ApiOperation("获取科室列表")
    @GetMapping("/depList")
    public Result getDepList() {
        return this.departmentService.getList();
    }
    @ApiOperation("根据ID查询科室")
        @GetMapping("/findDepartmentById/{id}")
    public Result queryDepartmentById(@PathVariable Long id) {
        System.out.println("id = " + id);
        Department department = this.departmentService.getById(id);
        return Result.ok(department);
    }

    @ApiOperation("修改科室")
    @PostMapping("/updateDepartment")
    public Result updateDepartment(@RequestBody Department department) {
        boolean isSuccess = this.departmentService.updateById(department);
        if (isSuccess) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("添加新科室")
    @PostMapping("addDepartment")
    public Result insertDepartment(@RequestBody Department department) {
        boolean saved = this.departmentService.save(department);
        if (saved) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation("删除科室")
    @DeleteMapping("removeDepartmentById/{id}")
    public Result removeDepartment(@PathVariable Long id) {
        boolean removed = this.departmentService.removeById(id);
        if (removed) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

}
